package com.example.demo.utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 集合工具类
 *
 * @company GeekPlus
 * @project beetle
 * @author 刘俊
 * @date 2016年11月25日
 * @since 1.0.0
 */
public final class CollectionUtil {
    private CollectionUtil() {}

    public static void erase(List<?> list, int begin, int end) {
        if (begin < 0 || end < 0 || begin > end || list.size() <= begin) {
            return;
        }
        int loop = end - begin;
        for (int i = 0; i < loop; ++i) {
            list.remove(begin);
        }
    }

    public static boolean equals(Collection<?> coll1, Collection<?> coll2) {
        return coll1.containsAll(coll2) && coll2.containsAll(coll1);
    }

    /**
     * 判断集合是否为null或空集
     */
    public static boolean isEmpty(Collection<?> coll) {
        return coll == null || coll.size() == 0;
    }

    /**
     * 判断集合是否存在元素
     * @return
     */
    public static boolean isNotEmpty(Collection<?> coll) {
        return !isEmpty(coll);
    }

    /**
     * 判断Map是否为null或空集
     * @return
     */
    public static boolean isEmpty(Map<?, ?> map) {
        return map == null || map.size() == 0;
    }

    /**
     * 判断Map是否存在元素
     * @return
     */
    public static boolean isNotEmpty(Map<?, ?> map) {
        return !isEmpty(map);
    }

    /**
     * 深拷贝list
     */
    @SuppressWarnings("unchecked")
    public static <T> List<T> deepCopy(List<T> src) throws IOException, ClassNotFoundException {
        ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
        ObjectOutputStream out = new ObjectOutputStream(byteOut);
        out.writeObject(src);

        ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
        ObjectInputStream in = new ObjectInputStream(byteIn);
        List<T> dest = (List<T>) in.readObject();
        return dest;
    }
}

